﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Store_Keeper_Addnewstock : System.Web.UI.Page
{
    public static string catid;

    protected void Page_Load(object sender, EventArgs e)
    {
        ASPxLabel2.Visible = false;
        string result = Request.QueryString.ToString();
        if (result != "")
        {
            ASPxLabel2.Text = "Product added Succesfully";
            ASPxLabel2.Visible = true;
        }
        
        if (!IsPostBack)
        {
            ViewState["index"] = 0;
          
        }
    }

  

    public void setAlldata(int index)
    {
        string poname = DropDownList4.Text;
       string proid= Bestlife.ERP.BusinessRules.B_PurchaseOrder.getproid(poname);

        Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();
        pro = Bestlife.ERP.BusinessRules.B_PurchaseOrder.setalldata(proid);
        subcatname.Text = pro.Sub_Cat_Name1;
        catname.Text = pro.Cat_Name1;
        brand.Text = pro.Brand;
        Productid.Text = pro.Pro_ID;
        catid = pro.Cat_ID;


    }

    protected void retailprice_TextChanged(object sender, EventArgs e)
    {
          decimal pur=(decimal)(double.Parse(purchaseprice.Text));
             decimal ret=(decimal)(double.Parse(retailprice.Text));
            if (pur<=ret)
            {
                if (purchaseprice.Text == "0.00" || retailprice.Text == "0.00")
                {
                    ASPxLabel2.Visible = true;

                    ASPxLabel2.Text = "Prices Cannot be Zero";
                }
                else
                {
                    if (retailprice.Text != "0.00")
                    {
                        string user = Session["user"].ToString();
                        string proid = DropDownList4.Text;
                        decimal markprice = (decimal)(double.Parse(retailprice.Text));
                        decimal cost = (decimal)(double.Parse(purchaseprice.Text));
                        Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();
                        productprice.Text = Bestlife.ERP.BusinessRules.productBV_PV.setdisprice(cost, markprice).ToString();
                        decimal price = Convert.ToDecimal(productprice.Text);
                        pointvalue.Text = Bestlife.ERP.BusinessRules.productBV_PV.setPV(cost, price).ToString();
                        businesspoint.Text = Bestlife.ERP.BusinessRules.productBV_PV.setBV(cost, price).ToString();
                        Bestlife.ERP.BusinessRules.productBV_PV.insertpvbv(cost, markprice, proid, user);
                    }

                }
            }
                else
                {
                    ASPxLabel2.Text = "Purchase Price Should be less than Retail Price";
            ASPxLabel2.Visible = true;
                }
        }
    

    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {
            decimal pur=(decimal)(double.Parse(purchaseprice.Text));
             decimal ret=(decimal)(double.Parse(retailprice.Text));
            if (pur<=ret)
            {
            if (purchaseprice.Text == "0.00" || retailprice.Text == "0.00")
            {
                ASPxLabel2.Visible = true;

                ASPxLabel2.Text = "Prices Cannot be Zero";
            }
            else
            {
                string[] imageArr = new string[4];
                Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();

                pro.Bussiness_Value = (decimal)(double.Parse(businesspoint.Text));
                pro.Point_Value = (decimal)(double.Parse(pointvalue.Text));
                pro.Brand = brand.Text;
                pro.Pro_Code = ASPxTextBox5.Text;
                pro.Cat_Name1= catid;
                pro.Pro_ID = Productid.Text;
                pro.Pro_Name = Bestlife.ERP.BusinessRules.B_PurchaseOrder.getproname(Productid.Text);
                pro.Pro_Price = (decimal)(double.Parse(productprice.Text));
                pro.Sub_Cat_Name1 = subcatname.Text;
                pro.Status = "Show";
                pro.Pur_Price = (decimal)(double.Parse(purchaseprice.Text));
                pro.Ret_Price = (decimal)(double.Parse(retailprice.Text));


                Bestlife.ERP.BusinessRules.productBV_PV.insert(pro, user);

                //Images Upload
                // Get the HttpFileCollection
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(MapPath("~/C:/inetpub/vhosts/bestlifecrown.com/httpsdocs/web/img/productimage") + "\\" +
                          System.IO.Path.GetFileName(hpf.FileName));
                        string image = hpf.FileName;
                        imageArr[i] = image;

                    }
                }
                string pid = Productid.Text;
                Bestlife.ERP.BusinessRules.productBV_PV.insertimage(imageArr, pid);
                string ok = "ok";
                Response.Redirect("Addnewstock.aspx?a=ok");
            }
        }
            else
            {
                ASPxLabel2.Text = "Product added Succesfully";
                ASPxLabel2.Visible = true;
            }
        }
        catch
        {
            ASPxLabel2.Visible = true;
            ASPxLabel2.Text = "This product is alredy entered to system";
        }
    }

    protected void DropDownList4_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int index = int.Parse(ViewState["index"].ToString());
        setAlldata(index);

        string proname = DropDownList4.Text;
        ASPxTextBox3.Text = Bestlife.ERP.BusinessRules.B_PurchaseOrder.laststockid().ToString();
        string proid = Bestlife.ERP.BusinessRules.B_PurchaseOrder.getproid(proname);
        decimal i = Bestlife.ERP.BusinessRules.B_PurchaseOrder.lastcode(proid);

        ASPxTextBox5.Text = i.ToString();
    }
   
}